#include "MyLogger.hh"
#include <iostream>
#include <log4cpp/Category.hh>
#include <log4cpp/Priority.hh>
#include <log4cpp/PatternLayout.hh>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/FileAppender.hh>
using std::cout;
using std::endl;
using namespace log4cpp;

MyLogger * MyLogger::_pInstance = nullptr;

MyLogger * MyLogger::getInstance(){
    if(_pInstance == nullptr){
        _pInstance = new MyLogger();
    }
    return _pInstance;
}
void MyLogger::destroy(){
    if(_pInstance){
        delete _pInstance;
        _pInstance = nullptr;
    }
}
void MyLogger::warn(const char *msg){
    _mycat.warn(msg);
}
void MyLogger::error(const char *msg){
    _mycat.error(msg);
}
void MyLogger::debug(const char *msg){
    _mycat.debug(msg);
}
void MyLogger::info(const char *msg){
    _mycat.info(msg);
}

MyLogger::MyLogger()
: _mycat(Category::getRoot().getInstance("mycat"))
{
    //日志布局
    PatternLayout * ptn1 = new PatternLayout();
    ptn1->setConversionPattern("%d %c [%p] %m%n");

    PatternLayout * ptn2 = new PatternLayout();
    ptn2->setConversionPattern("%d %c [%p] %m%n");

    //日志目的地
    auto pos = new OstreamAppender("console",&cout);
    pos->setLayout(ptn1);

    auto pfile = new FileAppender("fileApp","wd.log");
    pfile->setLayout(ptn2);

    //设置优先级
    _mycat.setPriority(Priority::DEBUG);
    //设置目的地
    _mycat.addAppender(pos);
    _mycat.addAppender(pfile);
    cout << "MyLogger()" << endl;
}

MyLogger::~MyLogger(){
    Category::shutdown();
    cout << "~MyLogger()" << endl;
}

